package com.example.smartcity.Controller;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

@RestController
@RequestMapping("/ai")
public class FileUploadController {

    private final Path fileStorageLocation = Paths.get("uploads");

    @PostMapping("/upload")
    public String upload(MultipartFile uploadfile,String name) throws IOException {

        //1、输出测试
        System.out.println("==============");
        System.out.println(uploadfile);
        System.out.println(name);
        System.out.println(uploadfile.getName());
        System.out.println(uploadfile.getSize());
        //2、将上传的图片存储到硬盘
        InputStream inputStream = uploadfile.getInputStream();
        FileChannel inChannel = (FileChannel) Channels.newChannel(inputStream);
        FileChannel outChannel = new FileOutputStream("./a.jpg").getChannel();//当前目录下，命名为a.jpg
        inChannel.transferTo(0,inChannel.size(),outChannel);

        //3、关闭流
        inChannel.close();
        outChannel.close();
        inputStream.close();

        //4、返回成功信息
        return "success";
    }


}



